﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using TallyBook.BLL.Service;
using TallyBook.BLL.Service.CommonImp;
using TallyBook.BLL.Service.Imp;

namespace TallyBook.UI
{
    public partial class AccountControl : UserControl
    {
        public static List<Account> Account = new List<Account>();
        private readonly IAccountService<Account> _account;

        public AccountControl(IAccountService<Account> account)
        {
            _account = account;
            InitializeComponent();
        }

        private void AccountControl_Load(object sender, EventArgs e)
        {
            GetAll();
        }

        private void btnModification_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show(@"未选择");
                return;
            }
            var getcell = dataGridView1.CurrentRow.Index;
            var account = new Account
            {
                Id = long.Parse(dataGridView1.Rows[getcell].Cells["Id"].Value.ToString()),
                InitMoney = decimal.Parse(dataGridView1.Rows[getcell].Cells["InitMoney"].Value.ToString()),
                CurrentMoney = decimal.Parse(dataGridView1.Rows[getcell].Cells["CurrentMoney"].Value.ToString()),
                Comment = dataGridView1.Rows[getcell].Cells["Comment"].Value.ToString(),
                Name = dataGridView1.Rows[getcell].Cells["Name"].Value.ToString(),
                AccountTypeId = Convert.ToInt32(dataGridView1.Rows[getcell].Cells["AccountTypeId"].Value.ToString()),
            };

            var form = new AccountModification
            {
                Account = account,
                RefreshUpdateForm = GetAll
            };
            form.ShowDialog(this);
        }

        //根据主键Id查询
        private void btnGet_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(idSelect.Text))
            {
                MessageBox.Show(@"请输入账户 --");
                return;
            }
            long id = long.Parse(idSelect.Text);

            if (_account.Get(id) != null)
            {
                var get = new List<Account> {_account.Get(id)};
                dataGridView1.DataSource = get.ToArray();
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            var form = new AddAccountForm(new AccountService())
            {
                RefreshMainForm = GetAll
            };
            form.ShowDialog(this);
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow == null)
            {
                MessageBox.Show(@"未选择");
                return;
            }
            var getcell = dataGridView1.CurrentRow.Index;
            _account.Delete(Convert.ToInt64(dataGridView1.Rows[getcell].Cells[0].Value.ToString()));
            GetAll();
        }

        /// <summary>
        ///     公共方法
        /// </summary>
        public void GetAll()
        {
            dataGridView1.DataSource = null;
            var re = new DataGridViewService();
            dataGridView1.Columns.AddRange(re.BindColumns(typeof (Account)));
            List<Account> getall = _account.GetAll();
            dataGridView1.DataSource = getall.ToArray();
        }

        ///// <summary>
        /////     List数组添加数据方法。
        ///// </summary>
        ///// <param name="accounts"></param>
        //private void InserData_Method(List<string> accounts)
        //{
        //    Account.Add(new Account
        //    {
        //        Id = long.Parse(accounts[0]),
        //        Name = accounts[1],
        //        CurrentMoney = decimal.Parse(accounts[2]),
        //        InitMoney = decimal.Parse(accounts[3]),
        //        Comment = accounts[4]
        //    });
        //    var re = new DataGridViewService();
        //    dataGridView1.DataSource = null;
        //    dataGridView1.Columns.AddRange(re.BindColumns(typeof(Account)));
        //    dataGridView1.DataSource = Account;
        //}
    }
}